function Api () {
  this.vue
}
Api.prototype = {
  setVue: function (vue) {
    this.vue = vue
  },
  async getArticleAction () {
    const url = this.$config.urls.get('load_article_action')
    const { data: res } = await this.$http.post(url, this.Param)
    if (res.code !== 0) {
      console.log('请求失败！')
      console.log(res)
    } else {
      // 将数据设置到vuex
      // console.log(res.data)
      this.setArticleActionCommentCount(res.data.comment_count)
      this.setArticleActionCollectionCount(res.data.collection_count)
      this.setArticleActionFabulousCount(res.data.fabulous_count)
      this.setArticleActionViewCount(res.data.view_count)
    }
  },
  // 保存展现行为数据
  loadinfo: function (articleId) {
    const url = this.vue.$config.urls.get('load_article_info')
    return new Promise((resolve, reject) => {
      this.vue.$http.post(url, { article_id: articleId }).then((d) => {
        resolve(d)
      }).catch((e) => {
        reject(e)
      })
    })
  },
  // 加载文章关系信息
  loadbehavior: function (articleId, authorId) {
    const url = this.vue.$config.urls.get('load_article_behavior')
    return this.vue.$store.getEquipmentId().then(equipmentId => {
      return new Promise((resolve, reject) => {
        this.vue.$http.post(url, { equipment_id: equipmentId, article_id: articleId, author_id: authorId }).then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
      })
    }).catch(e => {
      return new Promise((resolve, reject) => {
        reject(e)
      })
    })
  },
  // 喜欢、点赞
  like: function (data) {
    const url = this.vue.$config.urls.get('like_behavior')
    return this.vue.$store.getEquipmentId().then(equipmentId => {
      return new Promise((resolve, reject) => {
        this.vue.$http.post(url, { equipment_id: equipmentId, entry_id: data.articleId, type: 0, operation: data.operation }).then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
      })
    }).catch(e => {
      return new Promise((resolve, reject) => {
        reject(e)
      })
    })
  },
  // 点赞、取消赞
  like2: function (data) {
    // 去articleActionList找到对应的articleId,找到后将文章id，是否点赞
    console.log(this.articleActionList)
    this.articleActionList.forEach(item => {
      if (item.id === data.articleId) {
        data.articleId = item.id
        data.operation = item.isLike ? 1 : 0
      }
    })
    const url = this.$config.urls.get('like_behavior')
    return new Promise((resolve, reject) => {
      this.vue.$http.post(url, { article_id: data.articleId, type: 0, operation: data.operation })
        .then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
        .then(d => {
          if (d.data.code === 0) {
            if (data.operation === 1) {
              this.subArticleActionListFabulousCount(data.articleId)
            } else {
              this.addArticleActionListFabulousCount(data.articleId)
            }
            this.reverseArticleActionListConfigFabulousCount(data.articleId)
          } else {
            console.log(d)
            this.$message.error(d.data.error_message)
          }
        }).catch(e => {
          console.log(e)
        })
    })
  },
  // 不喜欢
  unlike: function (data) {
    const url = this.vue.$config.urls.get('unlike_behavior')
    return this.vue.$store.getEquipmentId().then(equipmentId => {
      return new Promise((resolve, reject) => {
        this.vue.$http.post(url, { equipment_id: equipmentId, article_id: data.articleId, type: data.type }).then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
      })
    }).catch(e => {
      return new Promise((resolve, reject) => {
        reject(e)
      })
    })
  },
  // 阅读
  read: function (data) {
    const url = this.vue.$config.urls.get('read_behavior')
    return this.vue.$store.getEquipmentId().then(equipmentId => {
      return new Promise((resolve, reject) => {
        this.vue.$http.post(url, {
          equipment_id: equipmentId,
          article_id: data.articleId,
          count: 1,
          read_duration: data.readDuration,
          percentage: data.percentage,
          load_duration: data.loadDuration
        }).then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
      })
    }).catch(e => {
      return new Promise((resolve, reject) => {
        reject(e)
      })
    })
  },
  // 收藏
  collection: function (data) {
    const url = this.vue.$config.urls.get('collection_behavior')
    return this.vue.$store.getEquipmentId().then(equipmentId => {
      return new Promise((resolve, reject) => {
        this.vue.$http.post(url, {
          equipment_id: equipmentId,
          entry_id: data.articleId,
          published_time: data.publishedTime,
          type: 0,
          operation: data.operation
        }).then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
      })
    }).catch(e => {
      return new Promise((resolve, reject) => {
        reject(e)
      })
    })
  },
  // 转发
  forward: function (data) {
    const url = this.vue.$config.urls.get('forward_behavior')
    return this.vue.$store.getEquipmentId().then(equipmentId => {
      return new Promise((resolve, reject) => {
        this.vue.$http.post(url, {
          equipment_id: equipmentId,
          article_id: data.articleId
        }).then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
      })
    }).catch(e => {
      return new Promise((resolve, reject) => {
        reject(e)
      })
    })
  },
  // 分享
  share: function (data) {
    const url = this.vue.$config.urls.get('share_behavior')
    return this.vue.$store.getEquipmentId().then(equipmentId => {
      return new Promise((resolve, reject) => {
        this.vue.$http.post(url, {
          equipment_id: equipmentId,
          article_id: data.articleId,
          type: data.type
        }).then((d) => {
          resolve(d)
        }).catch((e) => {
          reject(e)
        })
      })
    }).catch(e => {
      return new Promise((resolve, reject) => {
        reject(e)
      })
    })
  },
  // 关注
  follow: function (data) {
    const url = this.vue.$config.urls.get('user_follow')
    return new Promise((resolve, reject) => {
      this.vue.$http.post(url, {
        author_id: data.authorId,
        operation: data.operation,
        article_id: data.articleId
      }).then((d) => {
        resolve(d)
      }).catch((e) => {
        reject(e)
      })
    })
  }
}

export default new Api()
